package site.wetsion.employ.server.service;

import java.math.BigInteger;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import site.wetsion.employ.server.mapper.CampusMapper;
import site.wetsion.employ.server.pojo.CampusNews;
import site.wetsion.employ.server.pojo.CampusNewsDetail;
import site.wetsion.employ.server.util.Page;

@Service
public class CampusService {
	
	private final static Logger log = LoggerFactory.getLogger(CampusService.class);

	
	@Autowired
	private CampusMapper campusMapper;
	
	/** 插入公告以及详情*/
	public boolean insertCampusNews(CampusNews news){
		CampusNewsDetail detail = news.getDetail();
		try {
			campusMapper.insertCampusNewsDetail(detail);
		} catch (Exception e) {
			log.error("插入公告详情失败", e);
			return false;
		}
		try {
			news.setNews_detail_id(detail.getId());
			campusMapper.insertCampusNews(news);
			return true;
		} catch (Exception e) {
			log.error("插入公告失败", e);
			try {
				campusMapper.deleteCampusNewsDetailById(detail.getId());
			} catch (Exception e1) {
				log.error("回滚删除公告详情失败", e1);
			}
			return false;
		}
	}
	
	/** 按分类分页获取公告列表*/
	public List<CampusNews> getCampusNewsListByTypeAndPage(Page page, int type){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			return campusMapper.getCampusNewsListByTypeAndPage((pageNum-1)*pageSize, pageSize, type);
		} catch (Exception e) {
			log.error("获取公告列表失败，类型="+type, e);
			return null;
		}
	}
	
	/** 按分页获取公告列表*/
	public List<CampusNews> getCampusNewsListByPage(Page page){
		int pageNum = page.getPageNum();
		int pageSize = page.getPageSize();
		try {
			return campusMapper.getCampusNewsListByPage((pageNum-1)*pageSize, pageSize);
		} catch (Exception e) {
			log.error("获取公告列表失败", e);
			return null;
		}
	}
	
	public int getCountOfNewsByType(int type){
		try {
			return campusMapper.getCampusNewsCountByType(type);
		} catch (Exception e) {
			log.error("根据类型获取公告失败，type="+type, e);
			return 0;
		}
	}
	public int getCountOfNews(){
		try {
			return campusMapper.getCampusNewsCount();
		} catch (Exception e) {
			log.error("根据类型获取公告失败", e);
			return 0;
		}
	}
	
	/** 获取公告及详情*/
	public CampusNews getCampusNewsById(BigInteger id){
		CampusNews campusNews ;
		try {
			campusNews = campusMapper.getCampusNewsById(id);
		} catch (Exception e) {
			log.error("根据公告id获取公告失败", e);
			return null;
		}
		CampusNewsDetail detail ;
		try {
			detail = campusMapper.getCampusNewsDetailById(campusNews.getNews_detail_id());
			campusNews.setDetail(detail);
			return campusNews;
		} catch (Exception e1) {
			log.error("根据详情id获取公告详情失败", e1);
			return null;
		}
	}
	
	public boolean deleteCampusNewsById(BigInteger id){
		CampusNews news;
		try {
			news = campusMapper.getCampusNewsById(id);
		} catch (Exception e1) {
			log.error("获取新闻失败", e1);
			return false;
		}
		BigInteger detailId = news.getNews_detail_id();
		try {
			campusMapper.deleteCampusNewsById(id);
		} catch (Exception e) {
			log.error("删除公告失败", e);
			return false;
		}
		try {
			campusMapper.deleteCampusNewsDetailById(detailId);
		} catch (Exception e) {
			log.error("删除公告详情失败", e);
			return false;
		}
		return true;
	}

	/** 修改公告及详情*/
	public boolean modifyCampusNews(CampusNews campusNews){
		try {
			campusMapper.updateCampusNews(campusNews);
		} catch (Exception e) {
			log.error("更新修改失败", e);
			return false;
		}
		try {
			campusMapper.updateCampusNewsDetail(campusNews.getDetail());
		} catch (Exception e1) {
			log.error("更新修改公告详情失败", e1);
			return false;
		}
		return true;
	}
	
	/**
	 * 更新公告浏览量
	 * @param id 公告id
	 * @return
	 */
	public boolean updateNewsSee(BigInteger id){
		try {
			campusMapper.updateCampusNewsSee(id);
			return true;
		} catch (Exception e) {
			log.error("更新公告浏览量失败", e);
			return false;
		}
	}
	
}
